﻿using System;
using System.Collections.Generic;
using System.Text;

using com.dz.ct.db;
using com.dz.ct.common;
using System.Data;

namespace com.dz.ct.business
{
    public class DBManager
    {
        BookDao m_book_dao = new BookDao();

        /// <summary>
        /// 管理书籍站点
        /// </summary>
        /// <param name="i_book_site">站点信息</param>
        /// <param name="i_type">1、添加 2 修改</param>
        /// <returns></returns>
        public int UpdateSmallBookSite(SmallSiteInfo i_book_site, int i_type)
        {
            if (string.IsNullOrEmpty(i_book_site.SiteEnconding)
                || string.IsNullOrEmpty(i_book_site.SiteName)
                || string.IsNullOrEmpty(i_book_site.BookListUrl))
                return Consts.Faild;

            try
            {
                return m_book_dao.UpdateSmallBookSite(i_book_site, i_type);
            }
            catch (Exception ex)
            {
                LogHelper.Error("修改站点配置失败" + ex.ToString());
                return Consts.Faild;
            }
        }

        /// <summary>
        /// 添加书的信息
        /// </summary>
        public long InsertBookInfo(BookInfo i_book)
        {
  
            if (string.IsNullOrEmpty(i_book.BookName))
                return Consts.Faild;

            try
            {
                return m_book_dao.InsertBookInfo(i_book);
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加书籍信息失败" + ex.ToString());
                return Consts.Faild;
            }
        }

            /// <summary>
        /// 添加章节信息
        /// </summary>
        /// <param name="i_volume_name"></param>
        /// <param name="i_chapter_name"></param>
        /// <param name="i_update_time"></param>
        /// <param name="i_is_vip"></param>
        /// <param name="i_words_count"></param>
        /// <param name="i_book_id"></param>
        /// <param name="i_site_id"></param>
        /// <returns></returns>
        public long InsertBookChapter(BookChapterInfo i_book_chapter_info, string i_last_chapter_name)
        {
            if (string.IsNullOrEmpty(i_book_chapter_info.ChapterName))
                return Consts.Faild;

            try
            {
                return m_book_dao.InsertBookChapter(i_book_chapter_info, i_last_chapter_name);
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加章节信息失败" + ex.ToString());
                return Consts.Faild;
            }
        }

           /// <summary>
        /// 插入书籍评论
        /// </summary>
        /// <param name="i_review_content"></param>
        /// <returns></returns>
        public long InsertBookReview(long i_book_id, string i_review_content)
        {
            if (string.IsNullOrEmpty(i_review_content))
                return Consts.Faild;
            if (i_book_id <= 0)
                return Consts.Faild;

            try
            {
                return m_book_dao.InsertBookReview(i_book_id, i_review_content);
            }
            catch (Exception ex)
            {
                LogHelper.Error("插入书籍评论失败" + ex.ToString());
                return Consts.Faild;
            }
        }

         /// <summary>
        /// 获取不存在封面的书籍
        /// </summary>
        /// <returns></returns>
        public DataSet GetNoBookICONBookList()
        {
            try
            {
                return m_book_dao.GetNoBookICONBookList();
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取不存在封面的书籍失败" + ex.ToString());
                return null;
            }
        }

           /// <summary>
        /// 获取站点列表
        /// </summary>
        /// <returns></returns>
        public DataSet GetSmallBookSiteList()
        {
            try
            {
                return m_book_dao.GetSmallBookSiteList();
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取站点列表失败" + ex.ToString());
                return null;
            }
        }

           /// <summary>
        /// 删除站点
        /// </summary>
        /// <param name="i_site_id"></param>
        /// <returns></returns>
        public int DeleteSmallSite(int i_site_id)
        {
            try
            {
                return m_book_dao.DeleteSmallSite(i_site_id);
            }
            catch (Exception ex)
            {
                LogHelper.Error("删除站点失败" + ex.ToString());
                return -1;
            }
        }

         /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="i_str_sql"></param>
        /// <returns></returns>
        public DataSet QueryData(string i_str_sql)
        {
            try
            {
                return m_book_dao.QueryData(i_str_sql);
            }
            catch (Exception ex)
            {
                LogHelper.Error("查询数据失败" + i_str_sql + ex.ToString());
                return null;
            }
        }

        /// <summary>
        /// 添加小说收集信息
        /// </summary>
        /// <param name="i_book_id"></param>
        /// <param name="i_chapter_url"></param>
        /// <param name="i_site_id"></param>
        /// <returns></returns>
        public int AddBookSearch(string i_book_name, string i_chapter_url, int i_site_id)
        {
            try
            {
                return m_book_dao.AddBookSearch(i_book_name.Trim(), i_chapter_url.Trim(), i_site_id);

            }
            catch (Exception ex)
            {
                LogHelper.Error("添加小说收集信息失败" + ex.ToString());
                return -1;
            }
        }
    }
}
